package com.ibiaoke.goods.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.dao.PtSpecialCustomerDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.PtSpecialCustomer;
import com.ibiaoke.goods.dao.PtGoodsDao;
import com.ibiaoke.goods.entity.PtGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;



@Service("ptGoodService")
public class PtGoodServiceImpl implements  PtGoodService{

    @Autowired
    private PtGoodsDao ptgoodsDao;

    @Autowired
    private PtSpecialCustomerDao ptSpecialCustomerDao;

    /*Boss增加拼团商品*/
    @Override
    public int savePtGoods(PtGoods ptGood) {
        //保存商品至拼团商品，组织参数
        AssertUtils.notBlank(ptGood.getPtGoodsName(),"商品名称不能为空");

        AssertUtils.notBlank(ptGood.getParam(),"商品参数不能为空");

        AssertUtils.notBlank(ptGood.getDetail(),"商品详细不能为空");

        AssertUtils.notNull(ptGood.getPtType(),"拼团类型不能为空");

        AssertUtils.notNull(ptGood.getPtPrice(),"拼团价格不能为空");

        AssertUtils.notNull(ptGood.getPrice(),"原价不能为空");

        AssertUtils.notBlank(ptGood.getImage(),"商品主图不能为空");

        AssertUtils.notBlank(ptGood.getThumImage(),"商品缩略图不能为空");

        AssertUtils.notNull(ptGood.getStatus(),"商品状态不能为空");

        AssertUtils.notNull(ptGood.getStartTime(),"活动开始时间不能为空");

        AssertUtils.notNull(ptGood.getEndTime(),"活动结束时间不能为空");

        ptGood.setSaleNum(0);
        int count = ptgoodsDao.savePtGood(ptGood);
        return count;
    }

    @Override
    public List<PtGoods> listPtGoods(Page page,Customer customer) {
        String special_flag = "";
        List<PtGoods> ptGoodsList = null;
        if(customer != null){
            //获取当前用户的id，查询pt_special_customer表，
            PtSpecialCustomer ptSpecialCustomer = ptSpecialCustomerDao.selectByPrimaryKey(customer.getCustomerId());
            //如果可见用户组包含当前用户，那么把special_good设置为1
            if(ptSpecialCustomer != null){
                special_flag = "1";
            }
        }
        ptGoodsList = ptgoodsDao.listPtGoods(page,special_flag);
        return ptGoodsList;
    }

    @Override
    public PtGoods getGoodsById(Integer ptGoodsId) {
        AssertUtils.notNull(ptGoodsId,"商品Id不能为空");
        PtGoods ptGoods = ptgoodsDao.selectByPrimaryKey(ptGoodsId);
        return ptGoods;
    }

    /*Boss查询拼团商品*/
    @Override
    public List<PtGoods> getPtGood(String PtGoodName,Integer ptGoodId,Integer status,Page page) {
        List<PtGoods>  ptGoodList = ptgoodsDao.findPtGood(PtGoodName,ptGoodId,status,page);
        return ptGoodList;
    }

    /**
     * Boss通过id查询拼团商品
     * @param
     * @return
     */
   @Override
    public PtGoods getPtGoodsByPtId(Integer id) {
        AssertUtils.notNull(id,"商品Id不能为空");
        PtGoods ptGoods = ptgoodsDao.selectByPrimaryKey(id);
        return ptGoods;
    }


    /*Boss修改编辑拼团商品*/
    @Override
    public int editPtGood(PtGoods ptGood) {
        if(ptGood.getPtGoodsId() == null){
            throw new ServiceException("拼团商品的id不能为空");
        }
        return ptgoodsDao.updatePtGood(ptGood);
    }


    /*Boss删除拼团商品*/
    @Override
    public int updatePtGood(String PtGoodId) {
        if(PtGoodId == null){
            throw new ServiceException("商品id不能为空");
        }
        return ptgoodsDao.updatePtGood(PtGoodId);
    }
}
